


let EXPORTED_SYMBOLS = ["Prefs"];
            
let Prefs = (function () {
    "use strict";
    
    const Cu = Components.utils;
    function require(url) {
        let scope = {};
        Cu.import(url, scope);
        return scope;
    }
    let {Services} = require("resource://gre/modules/Services.jsm");
    
    let PreferencesMap = {
        AlertFoxEnabled: {name: "af-enabled", type: "boolean", defval: false},
        AlertFoxNoLocalTestWarning: {
            name: "af-no-local-test-warning", type: "boolean"
        },
        AlertFoxRuntimeValue: {name: "af-runtime-value", type: "integer", defval: 180},
        AlertFoxUserName: {name: "af-username", type: "string"},
        AlertFoxWarnCommands: {name: "af-warn-commands", type: "boolean", defval: true},
        AlertFoxWarnRuntime: {name: "af-warn-runtime", type: "boolean", defval: true},

        
        warnOnClearCookies: {name: "clearparam", type: "boolean", defval: true},
        
        shouldCloseSidebar: {name: "close-sidebar", type: "boolean"},
        defaultCommandsList: {name: "default-commands-list", type: "json",
                              defval: "{\"PROMPT\":\"^\\\\s*PROMPT\", \"PAUSE\":\"^\\\\s*PAUSE\", \"SET !ENCRYPTION (?:YES|TMP|TMPKEY)\":\"^\\\\s*SET !ENCRYPTION (?:YES|TMP|TMPKEY)\"}"},
        commandsList: {name: "commands-list", type: "json"},

        
        
        defaultDataPath: {name: "defdatapath", type: "string"},
        
        defaultDownloadPath: {name: "defdownpath", type: "string"},
        
        defaultLogPath: {name: "deflogpath", type: "string"},
        
        defaultMacroPath: {name: "defsavepath", type: "string"},
        
        
        replayingDelay: {name: "delay", type: "integer", defval: 0},
        
        encryptionType: {name: "encryptionType", type: "integer", defval: 1},
        
        useExpertMode: {name: "expert-mode", type: "boolean", defval: false},
        
        useExternalEditor: {name: "externaleditor", type: "boolean", defval: false},
        externalEditorPath: {name: "externaleditorpath", type: "string", defval: ""},

        
        highlightElement: {name: "highlight", type: "boolean", defval: true},
        
        preferIds: {name: "id-priority", type: "boolean", defval: true},
        
        maxMacroLength: {name: "maxMacroLength", type: "integer", defval: 2500},
        
        pageTimeout: {name: "maxwait", type: "integer", defval: 60},
        
        noWarningOnLoop: {name: "noloopwarning", type: "boolean", defval: false},
        
        openSidebarKbdShortcut: {name: "openiMacrosShortcut", type: "string", defval: "VK_F8"},
        
        profilerEnabled: {name: "profiler-enabled", type: "boolean"},
        
        recordingMode: {name: "record-mode", type: "string", defval: "auto"},
        
        scrollToElement: {name: "scroll", type: "boolean", defval: true},

        showPdfLinkDialog: {name: "show-pdflink-dialog", type: "boolean", defval: true},
        showTabCloseDialog: {name: "show-tab-close-dialog", type: "boolean", defval: true},
        visualizeJavascript: {name: "showjs", type: "boolean", defval: true},
        
        
        storeMacrosInProfile: {name: "store-in-profile", type: "boolean", defval: false},
        
        useToggleHotkey: {name: "use-toggle-hotkey", type: "boolean", defval: true},
        
        version: {name: "version", type: "string"},
        
        
        sitesWhiteList: {name: "white-list", type: "json", defval: "{\"iopus.com\":true, \"imacros.net\":true}"},
        isJustInstalled: {name: "just-installed", type: "boolean", defval: false},
        isJustUpgraded: {name: "just-upgraded", type: "boolean", defval: false},
    };

    let obj = new Object();

    obj.prefBranch = Services.prefs.getBranch("extensions.imacros.");

    for (let pref in PreferencesMap) {
        
        Object.defineProperty(obj, pref, {
            enumerable: true,

            get: ( function(pref_name) {
                return function() {
                    let p = PreferencesMap[pref_name];
                    try {
                        switch(p.type) {
                        case "string":
                            return obj.prefBranch.getCharPref(p.name);
                        case "integer":
                            return obj.prefBranch.getIntPref(p.name);
                        case "boolean":
                            return obj.prefBranch.getBoolPref(p.name);
                        case "json":
                            let s = obj.prefBranch.getCharPref(p.name);
                            if (s.startsWith("(") && s.endsWith(")")) {
                                
                                
                                s = s.slice(1, -1).replace("'", '"');
                                obj.prefBranch.setCharPref(p.name, s);
                            }
                            return JSON.parse(s);
                        }
                    } catch(e) {
                        
                        
                        
                        
                        
                        if (typeof p.defval != "undefined") {
                            if (p.type == "json")
                                return JSON.parse(p.defval);
                            else
                                return p.defval;
                        } else {
                            switch(p.type) {
                            case "string":
                                return "";
                            case "integer":
                                return NaN;
                            case "boolean":
                                return false;
                            case "json":
                                return null;
                            }
                        }
                    }
                };
            }) (pref),

            set: (function(pref_name) {
                return function(val) {
                    let p = PreferencesMap[pref_name];
                    switch(p.type) {
                    case "string":
                        obj.prefBranch.setCharPref(p.name, val.toString());
                        break;
                    case "integer":
                        obj.prefBranch.setIntPref(p.name, parseInt(val));
                        break;
                    case "boolean":
                        obj.prefBranch.setBoolPref(
                            p.name,
                            
                            typeof val == "boolean" ? val : !!val
                        );
                        break;
                    case "json":
                        obj.prefBranch.setCharPref(p.name, JSON.stringify(val));
                        break;
                    };
                };
            }) (pref)
        });
    }

    
    obj.clearAll = function() {
        [ "defsavepath", "defdatapath", "deflogpath",
          "defdownpath", "externaleditor",
          "externaleditorpath", "version",
          "close-sidebar", "toolbar-checked", 
          "scroll", "clickmode",
          "highlight", "showjs", "delay", "maxwait",
          "noloopwarning", "clearparam", "record-mode",
          "id-priority"].forEach((pref) => {
	      this.prefBranch.clearUserPref(pref);
	  });
    };

    return obj;
}) ();
